package com.longdaji.decoration.ui.detail;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.Comment;
import com.longdaji.decoration.data.bean.GoodsCommentBean;
import com.longdaji.decoration.data.bean.GoodsImagesBean;
import com.longdaji.decoration.data.bean.GoodsSkuBean;
import com.longdaji.decoration.data.bean.PayRequestBean;
import com.longdaji.decoration.ui.activitiesOfMine.collection.CollectionActivity;
import com.longdaji.decoration.ui.comment.CardRvAdapter;
import com.longdaji.decoration.ui.comment.CriticismActivity;
import com.longdaji.decoration.ui.main.MainActivity;
import com.longdaji.decoration.ui.news.InfoActivity;
import com.longdaji.decoration.ui.pay.PayActivity;
import com.longdaji.decoration.ui.wear.WearActivity;
import com.longdaji.decoration.utils.GlideImageLoader;
import com.longdaji.decoration.utils.ImageUtil;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.StringUtil;
import com.longdaji.decoration.widget.BetterRecycleView;
import com.longdaji.decoration.widget.BubbleDialog;
import com.longdaji.decoration.widget.BubbleLayout;
import com.longdaji.decoration.widget.CustomOperateDialog;
import com.longdaji.decoration.widget.CustomSnapHelper;
import com.longdaji.decoration.widget.ProductSkuDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.ShareBoardlistener;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class GoodsDetailActivity extends BaseActivity implements GoodsDetailContract.View, OnBannerListener {

    private static final String TAG = GoodsDetailActivity.class.getSimpleName();
    private List<String> images = new ArrayList<String>();
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_goods_name)
    TextView mTvGoodsName;
    @BindView(R.id.tv_goods_desc)
    TextView mTvGoodsDesc;
    @BindView(R.id.tv_money)
    TextView mTvPrice;
    @BindView(R.id.tv_comments_count)
    TextView mTvCommentsCount;
    @BindView(R.id.llyt_default_comment)
    LinearLayout mLlytDefaultComment;
    @BindView(R.id.tv_username)
    TextView mTvUserName;
    @BindView(R.id.iv_more)
    ImageView mIvMore;
    @BindView(R.id.tv_goods_comment_name)
    TextView mTvCommentsGoodsName;
    @BindView(R.id.tv_comments_like)
    TextView mTvGoodsLike;
    @BindView(R.id.tv_comment_content)
    TextView mTvCommentsContent;
    @BindView(R.id.tv_goods_attribute)
    TextView mTvGoodsAttribute;
    @BindView(R.id.iv_collect)
    ImageView mIvCollect;
    @BindView(R.id.iv_goods_detail_image)
    SubsamplingScaleImageView mIvDetailImage;
    @BindView(R.id.iv_head)
    ImageView mIvHead;
    @BindView(R.id.btn_wear)
    Button mBtnWear;
    @BindView(R.id.better_recycle_view)
    BetterRecycleView mBetterRecycleView;
    @BindView(R.id.tv_server)
    TextView mTvServer;

    @Inject
    GoodsDetailPresenter mPresenter;
    private List<Boolean> isVideos = new ArrayList<>();
    private String mGoodId;
    private ProductSkuDialog dialog;
    private GoodsSkuBean.Goods goods;
    private GoodsSkuBean.Goods.GoodsSku mSelectSku;
    private int mQuantity;
    private final static int CART = 1;
    private final static int WEAR = 2;
    private final static int BUY = 3;
    private Intent intent;
    private CustomOperateDialog mBubbleDialog;
    private Comment defaultComment;
    private CardRvAdapter mCardRvAdapter;
    private CustomShareListener mShareListener;
    private ShareAction mShareAction;
    private String mDetailImageAddr;
    private String mShareUrl;

    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, GoodsDetailActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "GoodsDetailActivity onCreate");
        setContentView(R.layout.activity_goods_detail);
        setUnBinder(ButterKnife.bind(this));
        initView();
        mPresenter.setView(this);
        if (getIntent() != null) {
            mGoodId = getIntent().getStringExtra("goods_id");
            getData(mGoodId);
        }
        // getData();
//        String[] urls = getResources().getStringArray(R.array.url);
//        List list = Arrays.asList(urls);
//        images = new ArrayList(list);
//        for (int i = 0; i < images.size(); i++) {
//            if (i == 0 || i == 1) {
//                isVideos.add(true);
//            } else {
//                isVideos.add(false);
//            }
//
//        }
//
//       // 简单使用
//        banner.setImages(images)
//                .setVideos(isVideos)
//                .isAutoPlay(false)
//                .setImageLoader(new GlideImageLoader())
//                .setOnBannerListener(this)
//                .start();

    }

    private void initView() {
        mBetterRecycleView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        mCardRvAdapter = new CardRvAdapter(this, new ArrayList<Comment.CommentImgs>());
        mBetterRecycleView.setAdapter(mCardRvAdapter);
        CustomSnapHelper mMySnapHelper = new CustomSnapHelper();
        try {
            mMySnapHelper.attachToRecyclerView(mBetterRecycleView);
        } catch (Exception e) {

        }

        mShareListener = new CustomShareListener(this);
    }

    private void getData(String goodsId) {
        Log.d(TAG, "getData goodsId is" + goodsId);
        mPresenter.getBannerImagesAndVideos(goodsId);
        mPresenter.getGoodsDetail(goodsId, Account.getInstance().getUserid());
        mPresenter.getGoodsDetailImage(goodsId);
        mPresenter.getGoodsComments(goodsId, Account.getInstance().getUserid());
        mPresenter.getDefaultComment(goodsId);
        mPresenter.getShareUrl();
        //mPresenter.getGoodsComments("8", "1eed917ecaac45339c2576cce7756e35");
    }


    @OnClick({R.id.iv_back, R.id.iv_more, R.id.btn_add_cart, R.id.tv_comments_more,
            R.id.rlyt_select_sku, R.id.tv_to_cart, R.id.iv_collect, R.id.tv_server,
            R.id.btn_buy, R.id.btn_wear, R.id.tv_comments_like, R.id.iv_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_wear:
                if (Account.getInstance().getUserid() == null) {
                    toast("请先登录账户！");
                    return;
                }
                if (mSelectSku == null) {
                    //toast("请选择商品规格！");
                    showSkuDialog(WEAR);
                } else {
                    Intent intent2 = WearActivity.getStartIntent(this);
                    intent2.putExtra("num", mQuantity);
                    intent2.putExtra("image", mSelectSku.getGoodsSkuImg());
                    startActivity(intent2);

                }

                break;
            case R.id.btn_buy:
                if (Account.getInstance().getUserid() == null) {
                    toast("请先登录账户！");
                    return;
                }
                if (mSelectSku == null) {
                    //toast("请选择商品规格！");
                    showSkuDialog(BUY);
                } else {
                    Intent intent3 = PayActivity.getStartIntent(this);
                    List<PayRequestBean> list = new ArrayList<>();
                    PayRequestBean payRequestBean = new PayRequestBean();
                    if (goods == null || mSelectSku == null) {
                        return;
                    }
                    payRequestBean.setGoodsId(mSelectSku.getGoodsId());
                    payRequestBean.setGoodsName(goods.getGoodsName());
                    payRequestBean.setGoodsPrice(mSelectSku.getPrice());
                    payRequestBean.setIsPackage("0");
                    payRequestBean.setProductNum(mQuantity);
                    payRequestBean.setSkuId(mSelectSku.getSkuId());
                    list.add(payRequestBean);
                    LogUtil.d(TAG, "pay request list=" + list);
                    double money = mQuantity * mSelectSku.getPrice();
                    intent3.putExtra("num", mQuantity);
                    intent3.putExtra("money", money);
                    intent3.putExtra("image", mSelectSku.getGoodsSkuImg());
                    intent3.putParcelableArrayListExtra("good_list", (ArrayList<? extends Parcelable>) list);
                    startActivity(intent3);
                }
                break;
            case R.id.iv_more:
                if (mBubbleDialog == null) {
                    BubbleLayout bl = new BubbleLayout(this);
                    bl.setBubbleColor(R.color.trans_black);
                    mBubbleDialog = new CustomOperateDialog(this)
                            .setClickedView(mIvMore)
                            .setPosition(BubbleDialog.Position.BOTTOM)
                            .setBubbleLayout(bl)
                            .calBar(true)
                            .setThroughEvent(false, true);
                    mBubbleDialog.setClickListener(new CustomOperateDialog.OnClickCustomButtonListener() {
                        @Override
                        public void onClick(int id) {
                            LogUtil.d(TAG, "mBubbleDialog onClick id=" + id);
                            switch (id) {
                                case R.id.tv_news:
                                    startActivity(InfoActivity.getStartIntent(GoodsDetailActivity.this));
                                    break;
                                case R.id.tv_home:
                                    intent = MainActivity.getStartIntent(GoodsDetailActivity.this);
                                    intent.putExtra("id", 1);
                                    startActivity(intent);
                                    break;
                                case R.id.tv_collect:
                                    startActivity(CollectionActivity.getStartIntent(GoodsDetailActivity.this));
                                    break;
                            }
                        }
                    });
                }
                mBubbleDialog.show();
                break;
            case R.id.btn_add_cart:
                showSkuDialog(CART);
                break;
            case R.id.tv_to_cart:
                Intent intent = MainActivity.getStartIntent(this);
                intent.putExtra("id", 2);
                startActivity(intent);
                break;
            case R.id.rlyt_select_sku:
                showSkuDialog(CART);
                break;
            case R.id.tv_comments_more:
                Intent intent1 = CriticismActivity.getStartIntent(this);
                intent1.putExtra("good_id", mGoodId);
                startActivity(intent1);
                break;
            case R.id.tv_comments_like:
                if (Account.getInstance().getUserid() == null) {
                    toast("请登录账户");
                    return;
                }
                if (defaultComment != null) {
                    if ("0".equals(defaultComment.getIsLike())) {
                        mPresenter.likeOrDislikeGoods(Account.getInstance().getUserid(), defaultComment.getCommentsId(), "1");
                    } else {
                        mPresenter.likeOrDislikeGoods(Account.getInstance().getUserid(), defaultComment.getCommentsId(), "0");
                    }

                }

                break;

            case R.id.iv_collect:
                if (TextUtils.isEmpty(Account.getInstance().getUserid())) {
                    toast("请先登录账户！");
                    return;
                }
                if (TextUtils.equals("1", goods.getIsCollection())) {
                    mPresenter.disCollectCurGoods(Account.getInstance().getUserid(), mGoodId);
                } else {
                    mPresenter.collectCurGoods(Account.getInstance().getUserid(), mGoodId);
                }
                break;
            case R.id.iv_share:
                if (mShareUrl == null) {
                    return;
                }
                mShareAction.open();
                break;
            case R.id.tv_server:
                Intent intent2 = InfoActivity.getStartIntent(this);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }

    @Override
    public void OnBannerClick(int position) {

    }


    @Override
    public void showBanner(List<GoodsImagesBean.GoodsImages> list) {
        Log.d(TAG, "showBanner list is" + list);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getVideoAddress() != null) {
                images.add(list.get(i).getVideoAddress());
                isVideos.add(true);
            } else {
                images.add(list.get(i).getImgAddress());
                isVideos.add(false);
            }

        }

        // 简单使用
        banner.setImages(images)
                .setVideos(isVideos)
                .isAutoPlay(false)
                .setImageLoader(new GlideImageLoader())
                .setOnBannerListener(this)
                .start();
    }

    @Override
    public void showGoodsDetail(GoodsSkuBean.Goods goodsSku) {
        Log.d(TAG, "showGoodsDetail is" + goodsSku);
        goods = goodsSku;
        if (goodsSku != null) {
            mTvGoodsName.setText(goodsSku.getGoodsName());
            mTvGoodsDesc.setText(goodsSku.getGoodsDesc());
            mTvPrice.setText("¥" + StringUtil.formatNumber(goodsSku.getGoodsDefaultPrice()));
            if (TextUtils.equals("1", goods.getIsCollection())) {
                mIvCollect.setImageResource(R.mipmap.icon_collect);

            } else {
                mIvCollect.setImageResource(R.mipmap.icon_uncollect);
            }

            mTvCommentsCount.setText("(" + goodsSku.getGoodsCommnets() + ")");
            if (goodsSku.getGoodsSku() != null && goodsSku.getGoodsSku().size() > 0) {
                mSelectSku = goodsSku.getGoodsSku().get(0);
            }
            if (mSelectSku != null) {
                mQuantity = 1;
                StringBuilder sb = new StringBuilder("已选：");
                for (GoodsSkuBean.Goods.GoodsSku.Attribute attribute : mSelectSku.getAttributes()) {
                    sb.append(attribute.getAttributeValue());
                    sb.append(" ");
                }
                mTvGoodsAttribute.setText(sb.toString());
                if ("1".equals(mSelectSku.getIsTry())) {
                    mBtnWear.setVisibility(View.VISIBLE);
                } else {
                    mBtnWear.setVisibility(View.GONE);

                }
            }
        }

        mPresenter.countVisitNum(goodsSku);

    }

//    @Override
//    public void showDefaultComment(GoodsCommentListBean goodsCommentListBean) {
//        int size = goodsCommentListBean.getCommentGoods().size();
//        mTvCommentsCount.setText("(" + size + ")");
//        if (size > 0) {
//            mLlytDefaultComment.setVisibility(View.VISIBLE);
//            defaultComment = goodsCommentListBean.getCommentGoods().get(0);
//            GlideUtils.loadImage(this, defaultComment.getHeadImgLocation(), mIvHead, null, R.drawable.no_banner, R.drawable.no_banner);
//            mTvUserName.setText(defaultComment.getUserName());
//            mTvCommentsGoodsName.setText(defaultComment.getGoodsName());
//            mTvGoodsLike.setText(defaultComment.getCommentLike() + "");
//
//            mCardRvAdapter.setData(null);
//            if (TextUtils.isEmpty(defaultComment.getCommentContent())) {
//                mTvCommentsContent.setVisibility(View.GONE);
//            } else {
//                mTvCommentsContent.setVisibility(View.VISIBLE);
//                mTvCommentsContent.setText(defaultComment.getCommentContent());
//            }
//
//            if ("0".equals(defaultComment.getIsLike())) {
//                mTvGoodsLike.setTextColor(ContextCompat.getColor(this, R.color.important_txt_color));
//                Drawable drawable = getResources().getDrawable(R.mipmap.before_like);
//                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//                mTvGoodsLike.setCompoundDrawables(drawable, null, null, null);
//            } else {
//                mTvGoodsLike.setTextColor(ContextCompat.getColor(this, R.color.red));
//                Drawable drawable = getResources().getDrawable(R.mipmap.after_like);
//                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//                mTvGoodsLike.setCompoundDrawables(drawable, null, null, null);
//            }
//        } else {
//            mLlytDefaultComment.setVisibility(View.GONE);
//        }
//
//    }

    @Override
    public void showDefaultComment(GoodsCommentBean goodsCommentBean) {
        defaultComment = goodsCommentBean.getGoodsComment();
        if (defaultComment != null) {
            mLlytDefaultComment.setVisibility(View.VISIBLE);
            //GlideUtils.loadImage(this, defaultComment.getHeadImgLocation(), mIvHead, null, R.drawable.no_banner, R.drawable.no_banner);
            //ImageUtil.loadIntoUseFitWidth(this, defaultComment.getHeadImgLocation(), R.drawable.no_banner, mIvHead);
            ImageUtil.loadCircleImage(this, defaultComment.getHeadImgLocation(), R.mipmap.default_head_portrait_mine, mIvHead);

//            Glide.with(this.getApplicationContext())
//                    .load("")
//                    //.diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                     .placeholder(R.drawable.no_banner)
//                    .error(R.drawable.no_banner)
//                    .into(mIvHead);
            mTvUserName.setText(defaultComment.getUserName());
            mTvCommentsGoodsName.setText(defaultComment.getGoodsName());
            // mTvGoodsLike.setText(defaultComment.getCommentLike() + "");
            if (defaultComment.getCommentImgs() != null && defaultComment.getCommentImgs().size() > 0) {
                mBetterRecycleView.setVisibility(View.VISIBLE);
                mCardRvAdapter.setData(defaultComment.getCommentImgs());
            } else {
                mBetterRecycleView.setVisibility(View.GONE);
            }

            if (TextUtils.isEmpty(defaultComment.getCommentContent())) {
                mTvCommentsContent.setVisibility(View.GONE);
            } else {
                mTvCommentsContent.setVisibility(View.VISIBLE);
                mTvCommentsContent.setText(defaultComment.getCommentContent());
            }

            if ("0".equals(defaultComment.getIsLike())) {
                mTvGoodsLike.setTextColor(ContextCompat.getColor(this, R.color.important_txt_color));
                Drawable drawable = getResources().getDrawable(R.mipmap.before_like);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                mTvGoodsLike.setCompoundDrawables(drawable, null, null, null);
            } else {
                mTvGoodsLike.setTextColor(ContextCompat.getColor(this, R.color.red));
                Drawable drawable = getResources().getDrawable(R.mipmap.after_like);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                mTvGoodsLike.setCompoundDrawables(drawable, null, null, null);
            }
        } else {
            mLlytDefaultComment.setVisibility(View.GONE);
        }
    }

    @Override
    public void showNoDefaultComment() {
        mLlytDefaultComment.setVisibility(View.GONE);
    }

    @Override
    public void showGoodsDetailImage(String imageAddress) {
        mDetailImageAddr = imageAddress;
        LogUtil.d(TAG, "showGoodsDetailImage imageAddress=" + imageAddress);
        // GlideUtils.loadImage(this, imageAddress, mIvDetailImage, null, R.drawable.no_banner);
//        Glide.with(this.getApplicationContext())
//                .load(imageAddress)
//                //.thumbnail(0.1f)
//                //.diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                //.placeholder(R.drawable.no_banner)
//                //.centerCrop()
//                .fitCenter()
//                .error(R.drawable.no_banner)
//                .into(mIvDetailImage);
        ImageUtil.loadIntoUseFitWidth(this.getApplicationContext(), imageAddress, mIvDetailImage);
        mIvDetailImage.setImage(ImageSource.uri(imageAddress));

    }

    @Override
    public void addToCartSuccess() {
        toast("添加购物车成功！");
    }

    @Override
    public void addToCartFail(String msg) {
        toast(msg);
    }

    @Override
    public void showLikeSuccess() {

        LogUtil.d(TAG, "showLikeSuccess");
        if (defaultComment != null) {
            mTvGoodsLike.setTextColor(ContextCompat.getColor(this, R.color.red));
            defaultComment.setIsLike("1");
            defaultComment.setCommentLike(defaultComment.getCommentLike() + 1);
            Drawable drawable = getResources().getDrawable(R.mipmap.after_like);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            mTvGoodsLike.setCompoundDrawables(drawable, null, null, null);
            mTvGoodsLike.setText(defaultComment.getCommentLike() + "");
        }
    }

    @Override
    public void showLikeFail() {
        toast("点赞失败！");
    }

    @Override
    public void showDisLikeSuccess() {
        LogUtil.d(TAG, "showDisLikeSuccess");
        if (defaultComment != null) {
            mTvGoodsLike.setTextColor(ContextCompat.getColor(this, R.color.important_txt_color));
            defaultComment.setIsLike("0");
            defaultComment.setCommentLike(defaultComment.getCommentLike() - 1);
            Drawable drawable = getResources().getDrawable(R.mipmap.before_like);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            mTvGoodsLike.setCompoundDrawables(drawable, null, null, null);
            mTvGoodsLike.setText(defaultComment.getCommentLike() + "");
        }
        //mPresenter.getGoodsComments("8", "1eed917ecaac45339c2576cce7756e35");

    }

    @Override
    public void showDisLikeFail() {
        toast("取消点赞失败！");
    }

    @Override
    public void showCollectSuccess() {
        toast("收藏成功");
        if (goods != null) {
            goods.setIsCollection("1");
        }
        // mIvCollect.setBackgroundResource(R.mipmap.icon_collect);
        mIvCollect.setImageResource(R.mipmap.icon_collect);
    }

    @Override
    public void showCollectFail() {
        toast("收藏失败");
    }

    @Override
    public void showDisCollectSuccess() {
        if (goods != null) {
            goods.setIsCollection("0");
        }
        mIvCollect.setImageResource(R.mipmap.icon_uncollect);
    }

    @Override
    public void showDisCollectFail() {
        toast("取消藏失败");
    }

    @Override
    public void showShareUrl(final String url) {
        if (url == null) {
            return;
        }
        mShareUrl = url;
                /*增加自定义按钮的分享面板*/
        mShareAction = new ShareAction(GoodsDetailActivity.this).setDisplayList(
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                .setShareboardclickCallback(new ShareBoardlistener() {
                    @Override
                    public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {

                        UMWeb web = new UMWeb(url);
                        web.setTitle("桃心");
                        web.setThumb(new UMImage(GoodsDetailActivity.this, R.mipmap.launcher_icon));
                        web.setDescription("一个专卖女生饰品的APP");
                        new ShareAction(GoodsDetailActivity.this).withMedia(web)
                                .setPlatform(share_media)
                                .setCallback(mShareListener).share();
                    }
                });

    }

    private void showSkuDialog(int type) {
        if (goods == null) {
            toast("获取商品信息失败！");
            return;
        }
        if (Account.getInstance().getUserid() == null) {
            toast("请先登录账户！");
            return;
        }
        dialog = new ProductSkuDialog(this);
        dialog.setData(type, goods, new ProductSkuDialog.Callback() {
            @Override
            public void onAdded(GoodsSkuBean.Goods.GoodsSku sku, int quantity, int type) {
                mSelectSku = sku;
                mQuantity = quantity;
                if (sku == null) {
                    return;
                }
                Log.d(TAG, "showSkuDialog onAdded sku =" + sku + "quantity=" + quantity);
                StringBuilder sb = new StringBuilder("已选：");
                for (GoodsSkuBean.Goods.GoodsSku.Attribute attribute : sku.getAttributes()) {
                    sb.append(attribute.getAttributeValue());
                    sb.append(" ");
                }
                mTvGoodsAttribute.setText(sb.toString());
                if ("1".equals(sku.getIsTry())) {
                    mBtnWear.setVisibility(View.VISIBLE);
                } else {
                    mBtnWear.setVisibility(View.GONE);

                }

                switch (type) {
                    case CART:
                        mPresenter.addGoodsSkuToCart(sku, quantity);
                        break;
                    case WEAR:
                        intent = WearActivity.getStartIntent(GoodsDetailActivity.this);
                        intent.putExtra("num", mQuantity);
                        intent.putExtra("image", mSelectSku.getGoodsSkuImg());
                        startActivity(intent);
                        break;
                    case BUY:
                        intent = PayActivity.getStartIntent(GoodsDetailActivity.this);
                        List<PayRequestBean> list = new ArrayList<>();
                        PayRequestBean payRequestBean = new PayRequestBean();
                        if (goods == null || mSelectSku == null) {
                            return;
                        }
                        payRequestBean.setGoodsId(mSelectSku.getGoodsId());
                        payRequestBean.setGoodsName(goods.getGoodsName());
                        payRequestBean.setGoodsPrice(mSelectSku.getPrice());
                        payRequestBean.setIsPackage("0");
                        payRequestBean.setProductNum(mQuantity);
                        payRequestBean.setSkuId(mSelectSku.getSkuId());
                        list.add(payRequestBean);
                        LogUtil.d(TAG, "pay request list=" + list);
                        double money = mQuantity * mSelectSku.getPrice();
                        intent.putExtra("num", mQuantity);
                        intent.putExtra("money", money);
                        intent.putExtra("image", mSelectSku.getGoodsSkuImg());
                        intent.putParcelableArrayListExtra("good_list", (ArrayList<? extends Parcelable>) list);
                        startActivity(intent);
                        break;
                    default:
                        mPresenter.addGoodsSkuToCart(sku, quantity);
                        break;
                }
            }

            @Override
            public void onCancel(GoodsSkuBean.Goods.GoodsSku sku, int quantity) {
                mSelectSku = sku;
                mQuantity = quantity;
                if (sku == null) {
                    return;
                }
                Log.d(TAG, "showSkuDialog onAdded sku =" + sku + "quantity=" + quantity);
                StringBuilder sb = new StringBuilder("已选：");
                for (GoodsSkuBean.Goods.GoodsSku.Attribute attribute : sku.getAttributes()) {
                    sb.append(attribute.getAttributeValue());
                    sb.append(" ");
                }
                mTvGoodsAttribute.setText(sb.toString());
                if ("1".equals(sku.getIsTry())) {
                    mBtnWear.setVisibility(View.VISIBLE);
                } else {
                    mBtnWear.setVisibility(View.GONE);

                }
            }
        });
        dialog.show();
    }

    private static class CustomShareListener implements UMShareListener {

        private WeakReference<GoodsDetailActivity> mActivity;

        private CustomShareListener(GoodsDetailActivity activity) {
            mActivity = new WeakReference(activity);
        }

        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        @Override
        public void onResult(SHARE_MEDIA platform) {

            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(mActivity.get(), " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                        && platform != SHARE_MEDIA.EMAIL
                        && platform != SHARE_MEDIA.FLICKR
                        && platform != SHARE_MEDIA.FOURSQUARE
                        && platform != SHARE_MEDIA.TUMBLR
                        && platform != SHARE_MEDIA.POCKET
                        && platform != SHARE_MEDIA.PINTEREST

                        && platform != SHARE_MEDIA.INSTAGRAM
                        && platform != SHARE_MEDIA.GOOGLEPLUS
                        && platform != SHARE_MEDIA.YNOTE
                        && platform != SHARE_MEDIA.EVERNOTE) {
                    Toast.makeText(mActivity.get(), " 分享成功啦", Toast.LENGTH_SHORT).show();
                }

            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            if (platform != SHARE_MEDIA.MORE && platform != SHARE_MEDIA.SMS
                    && platform != SHARE_MEDIA.EMAIL
                    && platform != SHARE_MEDIA.FLICKR
                    && platform != SHARE_MEDIA.FOURSQUARE
                    && platform != SHARE_MEDIA.TUMBLR
                    && platform != SHARE_MEDIA.POCKET
                    && platform != SHARE_MEDIA.PINTEREST

                    && platform != SHARE_MEDIA.INSTAGRAM
                    && platform != SHARE_MEDIA.GOOGLEPLUS
                    && platform != SHARE_MEDIA.YNOTE
                    && platform != SHARE_MEDIA.EVERNOTE) {
                Toast.makeText(mActivity.get(), " 分享失败啦", Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {

            Toast.makeText(mActivity.get(), " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 屏幕横竖屏切换时避免出现window leak的问题
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mShareAction.close();
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        UMShareAPI.get(this).release();
        super.onDestroy();
    }
}
